//
//  DiscoverDetailsController.m
//  Coco_Music
//
//  Created by 强哥 on 15/6/23.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//

#import "DiscoverDetailsController.h"
#import "DiscoverDetailsCell.h"
#import "AFN_block.h"
#import "Discover.h"
#import "UIScrollView+MJRefresh.h"
#import "DiscoverButtonController.h"




@interface DiscoverDetailsController ()

@property(nonatomic,retain)UICollectionView *collection;
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,assign)NSInteger pages;
@property(nonatomic,retain)DiscoverButtonController *SpecialC;

@end
//tablecell第二行详情页面
@implementation DiscoverDetailsController

-(void)dealloc
{
    [self.allSecondCellArray release];
    [self.collection release];
    [self.SpecialC release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.origin.x, [UIScreen mainScreen].bounds.origin.y, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-44-64) collectionViewLayout:flowLayout];
    self.collection.dataSource = self;
    self.collection.delegate = self;
    self.collection.backgroundColor = [UIColor whiteColor];
    self.allSecondCellArray = [NSMutableArray array];
    
    [self.collection registerClass:[DiscoverDetailsCell class] forCellWithReuseIdentifier:@"reuse"];
    
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width/2-20, self.view.frame.size.width/2-20);
    flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 20);
    flowLayout.footerReferenceSize = CGSizeMake(self.view.frame.size.width, 50);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 15, 20, 15);
    
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 80;
   
    self.page = 1;
    [self getInfo];
    [self.view addSubview:self.collection];
    [flowLayout release];
    [self.collection release];
    [self.SpecialC release];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中,请稍后";
    [self.HUD show:YES];
    self.collection.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getInfo)];
    
    
    
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DiscoverDetailsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.discover = [_allSecondCellArray objectAtIndex:indexPath.row];
    return cell;
}

-(void)getInfo
{
    if (self.collection.footer.isRefreshing == YES && self.page <=self.pages) {
        self.page++;
    }
 
        NSString *url = [NSString stringWithFormat:@"http://online.dongting.com/recomm/new_albums?page=%ld&size=30&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPone42C4&f=f320&s=s330&imsi=&hid=&splus=8.3&active=1&net=2&openudid=367f79a32ab2f14c1ee6d0a9e3587bcedc93d35c&idfa=8B711584-C712-4E8F-B6C8-39599E106A4C&utdid=VYJ5JD0tJEwDANcughrXcMK9&alf=201200&bundle_id=com.ttpod.music",self.page];
        [AFN_block NSString:url Blokc:^(id request) {
            NSMutableArray *array =  [request objectForKey:@"data"];
            self.pages = [[request objectForKey:@"pages"] integerValue];
            for (NSMutableDictionary *dic in array) {
                Discover *discover = [[Discover alloc]init];
                discover.SecondCellId = [dic objectForKey:@"_id"];
                discover.SecondCellPic_url = [dic objectForKey:@"pic_url"];
                discover.SecondCellTitle = [dic objectForKey:@"title"];
                discover.SecondCellYear = [dic objectForKey:@"year"];
                discover.SecondCellWeek = [dic objectForKey:@"week"];
                [_allSecondCellArray addObject:discover];
                [discover release];
            }
            [_collection reloadData];
            [self.HUD hide:YES];
            [_collection.footer endRefreshing];
        }];
}






-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
     self.SpecialC = [[DiscoverButtonController alloc]init];
    
    Discover *dis = [_allSecondCellArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:self.SpecialC animated:YES];
    self.SpecialC.DiscoverSecondPicUrl = dis.SecondCellPic_url;
    self.SpecialC.DiscoverSecondShowUrl = dis.SecondCellId;
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _allSecondCellArray.count;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
